<?php
/**
 * File: BaseModel.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-19 19:50
 */

namespace App\Models;

use awheel\MedooModel\MedooModel;
use PDO;

class BaseModel extends MedooModel
{
    public $database = 'default';

    public function __construct()
    {
        $config = [
            'default' => [
                'master' => [
                    [
                        'database_type' => 'mysql',
                        'database_name' => 'toupiao',
                        'prefix' => '',
                        'server' => '127.0.0.1',
                        'port' => '3306',
                        'username' => 'root',
                        'password' => '123456',
                        'charset' => 'utf8',
//                        'option' => array(
//                            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
//                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
//                        )
                    ]
                ],
                'slave' => [
                    [
                        'database_type' => 'mysql',
                        'database_name' => 'toupiao',
                        'prefix' => '',
                        'server' => '127.0.0.1',
                        'port' => '3306',
                        'username' => 'root',
                        'password' => '123456',
                        'charset' => 'utf8',
                    ]
                ]
            ]
        ];
        parent::__construct($config);
    }

    public function findByCondition($condition){
        return $this->get('*',$condition);
    }
    public function setProperty(array $dataArray)
    {
        $obj = new \ReflectionObject($this);

        foreach ($dataArray as $k => $v) {
            if (!$k || !is_string($k)) continue;
            if ($obj->hasProperty($k)) {
                $obj->getProperty($k)->setValue($this, $v);
            }
        }
        return $this;
    }
}